<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 16.03.14
 * Time: 18:29
 */
require_once BASE_DQ_PLUGIN_PATH . "classes/UserDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/GroupDao.php";

$userDao = new UserDao();
$groupDao = new GroupDao();

if (isset($_REQUEST["studentId"]) && intval($_REQUEST["studentId"]) > 0
    && isset($_REQUEST["groupId"]) && intval($_REQUEST["groupId"]) > 0
) {
    $studentId = intval($_REQUEST["studentId"]);
    $groupId = intval($_REQUEST["groupId"]);

    $student = $userDao->getById($studentId, UserDao::ROLE_STUDENT);
    $student->groups[0] = $groupId;
    $userDao->edit($student, UserDao::ROLE_STUDENT);

    header(
        "Location: " . menu_page_url("distant-quiz/admin.php", false) .
        "#students"
    );
}

$students = $userDao->getAll(UserDao::ROLE_STUDENT);
$groups = $groupDao->getAll();
?>

<table class="wp-list-table widefat fixed posts student-table">
    <thead>
    <tr>
        <th style="width: 5%;"><input name="select-all" type="checkbox"/></th>
        <th style="width: 5%;">Id</th>
        <th>Имя</th>
        <th>Группа</th>
    </tr>
    </thead>
    <tbody>
    <? foreach ($students as $student) { ?>
        <tr>
            <td><input type="checkbox"
                       name="students[]"
                       value="<?= $student->id ?>"/></td>
            <td><?= $student->id ?></td>
            <td><?= $student->displayName ?></td>
            <td>
                <? if (count($student->groups) > 0) { ?>
                    <span class="group-lbl"
                          title="Нажмите левой кнопкой мыши чтоб редактировать">
                        <?= $groupDao->getById(intval($student->groups[0]))->name; ?>
                    </span>
                <? } else { ?>
                    <span class="error-message"
                          title="Нажмите левой кнопкой мыши чтоб редактировать">
                    <?= "&lt;--Группа не назначена--&gt;"; ?>
                    </span>
                <? } ?>
                <select class="groups" style="display: none;">
                    <? foreach ($groups as $group) { ?>
                        <option value="<?= $group->id ?>"
                            <?=
                            (count($student->groups) > 0
                                && intval($student->groups[0]) == $group->id) ?
                                "selected='selected'" : "" ?>>
                            <?= $group->name ?>
                        </option>
                    <? } ?>
                </select>
            </td>
        </tr>
    <? } ?>
    </tbody>
</table>

<form name="student-form" method="post">
    <input type="hidden" name="studentId"/>
    <input type="hidden" name="groupId"/>
</form>

<script>
    $(function () {

    });
</script>